﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yottamud_2013_v3_Main
{
    public class InventoryManager
    {
        public struct InventoryType
        {
            public EquippedItems PlayerWornItems;
            public List<string> UnEquippedItems;
            public List<string> Keys;
            public int Encumbrance;
            public Currency Wealth;

            public struct Currency
            {
                public UInt32 Runic;
                public UInt32 Platinum;
                public UInt32 Gold;
                public UInt32 Silver;
                public UInt32 Copper;
            }

            public struct EquippedItems
            {
                public string Head;
                public string Eyes;
                public string Neck;
                public string Back;
                public string Ears;
                public string Torso;
                public string Arms;
                public string Hands;
                public string FingerOne;
                public string FingerTwo;
                public string WristOne;
                public string WristTwo;
                public string Waist;
                public string Legs;
                public string OffHand;
                public string Worn;
                public string MainWeapon;
                public string Feet;
                public string Readied;

            }
        }

        private const string NOTHING_ON = "Nothing Equiped";

        private static InventoryType ResetEquipment()
        {
            InventoryType pInventory = new InventoryType();

            pInventory.PlayerWornItems.Head = NOTHING_ON;
            pInventory.PlayerWornItems.MainWeapon = NOTHING_ON;
            pInventory.PlayerWornItems.Hands = NOTHING_ON;
            pInventory.PlayerWornItems.Back = NOTHING_ON;
            pInventory.PlayerWornItems.Neck = NOTHING_ON;
            pInventory.PlayerWornItems.Torso = NOTHING_ON;
            pInventory.PlayerWornItems.Ears = NOTHING_ON;
            pInventory.PlayerWornItems.Arms = NOTHING_ON;
            pInventory.PlayerWornItems.Feet = NOTHING_ON;
            pInventory.PlayerWornItems.FingerOne = NOTHING_ON;
            pInventory.PlayerWornItems.FingerTwo = NOTHING_ON;
            pInventory.PlayerWornItems.Legs = NOTHING_ON;
            pInventory.PlayerWornItems.OffHand = NOTHING_ON;
            pInventory.PlayerWornItems.Waist = NOTHING_ON;
            pInventory.PlayerWornItems.WristOne = NOTHING_ON;
            pInventory.PlayerWornItems.Worn = NOTHING_ON;
            pInventory.PlayerWornItems.Eyes = NOTHING_ON;
            pInventory.PlayerWornItems.WristTwo = NOTHING_ON;
            pInventory.PlayerWornItems.Readied = NOTHING_ON;

            pInventory.UnEquippedItems = new List<string>();
            pInventory.Keys = new List<string>();

            return pInventory;
        }

        private static string GetItemName(string iData)
        {
            string[] splitItem = iData.Split('(');
            return splitItem[0].Trim();
        }

        public static InventoryType DecodeInventoryItems(string iData)
        {
            InventoryType playerInventory = ResetEquipment();

            if (!iData.Contains("Nothing!"))
            {
                iData = iData.Replace("\r\n", " ");

                if (!iData.Contains("You have no keys"))
                {
                    int findKeysStartIndex = iData.IndexOf("You have the following keys:");
                    int findKeysEndIndex = iData.IndexOf("Wealth") - 2;

                    int findKeysLength = findKeysEndIndex - findKeysStartIndex;

                    if (findKeysLength > 0)
                    {
                        string[] keys = iData.Substring(findKeysStartIndex, findKeysLength).Replace("You have the following keys:", "").Trim().Split(',');

                        foreach (string key in keys)
                        {
                            playerInventory.Keys.Add(key.Trim());

                        }
                    }
                }

                int findInventoryEndIndex = iData.IndexOf("You have ");
                int findInventoryIndex = iData.IndexOf("carrying") + 8;
                int fingerCount = 0;
                int wristCount = 0;

                int findInventoryLength = findInventoryEndIndex - findInventoryIndex;

                if (findInventoryLength > 0)
                {
                    string inventory = iData.Substring(findInventoryIndex, findInventoryLength);

                    string[] splitInventory = inventory.Split(',');


                    foreach (string item in splitInventory)
                    {
                        if (!item.Contains("("))
                        {
                            playerInventory.UnEquippedItems.Add(item.Trim());
                        }

                        if (item.Contains("Readied"))
                        {
                            playerInventory.PlayerWornItems.Readied = GetItemName(item);
                        }

                        if (item.Contains("Two handed"))
                        {
                            playerInventory.PlayerWornItems.MainWeapon = GetItemName(item);
                        }

                        if (item.Contains("(Weapon Hand"))
                        {
                            playerInventory.PlayerWornItems.MainWeapon = GetItemName(item);
                        }

                        if (item.ToLower().Contains("runic"))
                        {
                            int runicStart = item.ToLower().IndexOf("runic");

                            playerInventory.Wealth.Runic = uint.Parse(item.Substring(0, runicStart).Trim());
                        }

                        if (item.Contains("platinum pieces"))
                        {
                            int platStart = item.IndexOf("platinum pieces");

                            UInt32 platinumPiece = Convert.ToUInt32(item.Substring(0, platStart).Trim(), 10);
                            playerInventory.Wealth.Platinum = platinumPiece;

                        }

                        if (item.Contains("gold crown"))
                        {
                            int goldStart = item.IndexOf("gold crown");

                            UInt32 goldPiece = Convert.ToUInt32(item.Substring(0, goldStart).Trim(), 10);
                            playerInventory.Wealth.Gold = goldPiece;
                        }

                        if (item.Contains("copper piece"))
                        {
                            int copperStart = item.IndexOf("copper piece");

                            UInt32 copperPiece = Convert.ToUInt32(item.Substring(0, copperStart).Trim(), 10);
                            playerInventory.Wealth.Copper = copperPiece;
                        }

                        if (item.Contains("silver noble"))
                        {
                            int silverStart = item.IndexOf("silver noble");

                            UInt32 silverPiece = Convert.ToUInt32(item.Substring(0, silverStart).Trim(), 10);
                            playerInventory.Wealth.Silver = silverPiece;
                        }

                        if (item.Contains("(Feet)"))
                        {
                            playerInventory.PlayerWornItems.Feet = GetItemName(item);
                        }

                        if (item.Contains("(Torso)"))
                        {
                            playerInventory.PlayerWornItems.Torso = GetItemName(item);
                        }

                        if (item.Contains("(Head)"))
                        {
                            playerInventory.PlayerWornItems.Head = GetItemName(item);
                        }

                        if (item.Contains("Finger") && fingerCount == 1)
                        {
                            playerInventory.PlayerWornItems.FingerTwo = GetItemName(item);
                        }

                        if (item.Contains("Arms"))
                        {
                            playerInventory.PlayerWornItems.Arms = GetItemName(item);
                        }

                        if (item.Contains("Back"))
                        {
                            playerInventory.PlayerWornItems.Back = GetItemName(item);
                        }

                        if (item.Contains("Neck"))
                        {
                            playerInventory.PlayerWornItems.Neck = GetItemName(item);
                        }

                        if (item.Contains("Legs"))
                        {
                            playerInventory.PlayerWornItems.Legs = GetItemName(item);
                        }

                        if (item.Contains("Waist"))
                        {
                            playerInventory.PlayerWornItems.Waist = GetItemName(item);
                        }

                        if (item.Contains("Off-Hand"))
                        {
                            playerInventory.PlayerWornItems.OffHand = GetItemName(item);
                        }

                        if (item.Contains("Wrist") && wristCount == 1)
                        {
                            playerInventory.PlayerWornItems.WristTwo = GetItemName(item);
                        }

                        if (item.Contains("Ears"))
                        {
                            playerInventory.PlayerWornItems.Ears = GetItemName(item);
                        }
                        if (item.Contains("Eyes"))
                        {
                            playerInventory.PlayerWornItems.Eyes = GetItemName(item);

                        }
                        if (item.Contains("Worn"))
                        {
                            playerInventory.PlayerWornItems.Worn = GetItemName(item);
                        }
                        if (item.Contains("Wrist") && wristCount == 0)
                        {
                            playerInventory.PlayerWornItems.WristOne = GetItemName(item);
                            wristCount++;
                        }
                        if (item.Contains("Finger") && fingerCount == 0)
                        {
                            playerInventory.PlayerWornItems.FingerOne = GetItemName(item);
                            fingerCount++;
                        }
                        if (item.Contains("Hands"))
                        {
                            playerInventory.PlayerWornItems.Hands = GetItemName(item);
                        }
                    }
                }
            }

            return playerInventory;
        }
    }
}
